package 冒泡排序;

public class BubbleSortOptimization {
    public static int[] bubbleSortOpt(int[] arr){
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            boolean flag = true;
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j+1]>arr[j]){
                    flag = false;
                    int t = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = t;
                }
            }
            //一趟下来是否位置发送变化
            if (flag)
                break;
        }
        return arr;
    }
}
